{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "impressed-accessory",
   "metadata": {},
   "source": [
    "## Seldon V2 Kubernetes Examples\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "91687b33",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"NAMESPACE\"] = \"seldon-mesh\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "sunrise-commercial",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'172.18.255.2'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MESH_IP=!kubectl get svc seldon-mesh -n ${NAMESPACE} -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "MESH_IP=MESH_IP[0]\n",
    "import os\n",
    "os.environ['MESH_IP'] = MESH_IP\n",
    "MESH_IP"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ae6fc5e",
   "metadata": {},
   "source": [
    "## Custom Server with Capabilities\n",
    "\n",
    "The `capabilities` field replaces the capabilities from the ServerConfig."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "19cea33d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Server\r\n",
      "metadata:\r\n",
      "  name: mlserver-134\r\n",
      "spec:\r\n",
      "  serverConfig: mlserver\r\n",
      "  capabilities:\r\n",
      "  - mlserver-1.3.4\r\n",
      "  podSpec:\r\n",
      "    containers:\r\n",
      "    - image: seldonio/mlserver:1.3.4\r\n",
      "      name: mlserver\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./servers/custom-mlserver-capabilities.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "293cbbf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io/mlserver-134 created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./servers/custom-mlserver-capabilities.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "283c3cc8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io/mlserver condition met\n",
      "server.mlops.seldon.io/mlserver-134 condition met\n",
      "server.mlops.seldon.io/triton condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s server --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5f028d19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: iris\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/mlserver/iris\"\r\n",
      "  requirements:\r\n",
      "  - mlserver-1.3.4\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/iris-custom-requirements.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9c274ddf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/iris-custom-requirements.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b88e05d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "76ae2b50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"iris_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"057ae95c-e6bc-4f57-babf-0817ff171729\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"np\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t2\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris --inference-host ${MESH_IP}:80 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b9872125",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"iris\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/iris-custom-server.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e07aa196",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io \"mlserver-134\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./servers/custom-mlserver.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31070ea2",
   "metadata": {},
   "source": [
    "## Custom Server with Extra Capabilities\n",
    "\n",
    "The `extraCapabilities` field extends the existing list from the ServerConfig."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0a7d7dc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Server\r\n",
      "metadata:\r\n",
      "  name: mlserver-134\r\n",
      "spec:\r\n",
      "  serverConfig: mlserver\r\n",
      "  extraCapabilities:\r\n",
      "  - mlserver-1.3.4\r\n",
      "  podSpec:\r\n",
      "    containers:\r\n",
      "    - image: seldonio/mlserver:1.3.4\r\n",
      "      name: mlserver\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./servers/custom-mlserver.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8aa3ac8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io/mlserver-134 created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./servers/custom-mlserver.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6c240ed2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io/mlserver condition met\n",
      "server.mlops.seldon.io/mlserver-134 condition met\n",
      "server.mlops.seldon.io/triton condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s server --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a2fc09f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: iris\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/mlserver/iris\"\r\n",
      "  server: mlserver-134\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/iris-custom-server.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d72cdf1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/iris-custom-server.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "226f5610",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e2da91a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"iris_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"a3e17c6c-ee3f-4a51-b890-6fb16385a757\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"np\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t2\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris --inference-host ${MESH_IP}:80 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0923dcf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"iris\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/iris-custom-server.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "db986727",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io \"mlserver-134\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./servers/custom-mlserver.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e694edee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
